/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.hxcx.top.modules.dvc.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.hxcx.top.common.config.Global;
import cn.hxcx.top.common.persistence.Page;
import cn.hxcx.top.common.utils.StringUtils;
import cn.hxcx.top.common.web.BaseController;
import cn.hxcx.top.modules.dvc.entity.DeviceInfo;
import cn.hxcx.top.modules.dvc.service.DeviceInfoService;
import cn.hxcx.top.modules.sys.utils.UserUtils;

/**
 * 设备信息相关Controller
 * @author DoubleX
 * @version 2019-07-10
 */
@Controller
@RequestMapping(value = "${adminPath}/dvc/deviceInfo")
public class DeviceInfoController extends BaseController {

	@Autowired
	private DeviceInfoService deviceInfoService;
	
	@ModelAttribute
	public DeviceInfo get(@RequestParam(required=false) String id) {
		DeviceInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = deviceInfoService.get(id);
		}
		if (entity == null){
			entity = new DeviceInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("dvc:deviceInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(DeviceInfo deviceInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DeviceInfo> page = deviceInfoService.findPage(new Page<DeviceInfo>(request, response), deviceInfo); 
		model.addAttribute("page", page);
		return "modules/dvc/deviceInfoList";
	}
	
	@RequiresPermissions("dvc:deviceInfo:view")
	@RequestMapping(value = {"mylist"})
	public String mylist(DeviceInfo deviceInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		deviceInfo.setDeviceManager(UserUtils.getUser());
		Page<DeviceInfo> page = deviceInfoService.findPage(new Page<DeviceInfo>(request, response), deviceInfo); 
		model.addAttribute("page", page);
		return "modules/dvc/deviceInfoList";
	}
	
	@RequiresPermissions("dvc:deviceInfo:view")
	@RequestMapping(value = {"showlist"})
	public String showlist(DeviceInfo deviceInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		deviceInfo.setDeviceStatus("1");
		Page<DeviceInfo> page = deviceInfoService.findPage(new Page<DeviceInfo>(request, response), deviceInfo); 
		model.addAttribute("page", page);
		return "modules/dvc/deviceInfoShowList";
	}

	@RequiresPermissions("dvc:deviceInfo:view")
	@RequestMapping(value = "form")
	public String form(DeviceInfo deviceInfo, Model model) {
		model.addAttribute("deviceInfo", deviceInfo);
		return "modules/dvc/deviceInfoForm";
	}
	
	@RequiresPermissions("dvc:deviceInfo:view")
	@RequestMapping(value = "detail")
	public String detail(DeviceInfo deviceInfo, Model model) {
		model.addAttribute("deviceInfo", deviceInfo);
		return "modules/dvc/deviceInfoDetail";
	}

	@RequiresPermissions("dvc:deviceInfo:edit")
	@RequestMapping(value = "save")
	public String save(DeviceInfo deviceInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, deviceInfo)){
			return form(deviceInfo, model);
		}
		deviceInfoService.save(deviceInfo);
		addMessage(redirectAttributes, "保存设备信息成功");
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceInfo/?repage";
	}
	
	@RequiresPermissions("dvc:deviceInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(DeviceInfo deviceInfo, RedirectAttributes redirectAttributes) {
		deviceInfoService.delete(deviceInfo);
		addMessage(redirectAttributes, "删除设备信息成功");
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceInfo/?repage";
	}

}